<style type="text/css">

    .demo-upload-list {
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
        margin-right: 4px;
    }

    .demo-upload-list img {
        width: 100%;
        height: 100%;
    }

    .demo-upload-list-cover {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .6);
    }

    .demo-upload-list:hover .demo-upload-list-cover {
        display: block;
    }

    .demo-upload-list-cover i {
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>
<template>
    <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <Form-item label="标题" prop="title">
            <i-input v-model="formValidate.title" placeholder="请输入标题"></i-input>
        </Form-item>
        <Form-item label="副标题" prop="Subtitle">
            <i-input v-model="formValidate.Subtitle" placeholder="请输入副标题"></i-input>
        </Form-item>

        <Form-item label="浏览数" prop="Viewcount">
            <i-input v-model="formValidate.Viewcount" placeholder="请输入浏览数"></i-input>
        </Form-item>
        <Form-item label="发布时间" >
            <Row>
                <i-col span="11">
                    <Date-picker type="datetime" placeholder="选择日期" :value="formValidate.Pubdate"  @on-change="changeEndDate"></Date-picker>
                </i-col>
            </Row>
        </Form-item>
        <Form-item label="新闻内容" prop="content">
            <div>
                <UE :defaultMsg=formValidate.Newscontent :config=config ref="ue"></UE>
            </div>
        </Form-item>
        <!--上传图片-->
        <Form-item label="封面" prop="image">
            <div class="demo-upload-list" v-for="item in imageList">
                <template v-if="item.status === 'finished'">
                    <img :src="apiUtilBase + item.response.Msg">
                    <div class="demo-upload-list-cover">
                        <Icon type="ios-eye-outline" @click="handleView(apiUtilBase + item.response.Msg)"></Icon>
                        <Icon type="ios-trash-outline" @click="handleRemove(item)"></Icon>
                    </div>
                </template>
                <template v-else>
                    <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
                </template>
            </div>
            <Upload ref="upload"
                    :show-upload-list="false"
                    :default-file-list="defaultList"
                    :on-success="handleSuccess"
                    :format="['jpg','jpeg','png']"
                    :max-size="2048"
                    :name="'uploadFile'"
                    :on-format-error="handleFormatError"
                    :on-exceeded-size="handleMaxSize"
                    :before-upload="handleBeforeUpload"
                    multiplelayout-content
                    type="drag"
                    :action="upLoadFile"
                    style="display: inline-block;width:58px;">
                <div style="width: 58px;height:58px;line-height: 58px;">
                    <Icon type="camera" size="20"></Icon>
                </div>
            </Upload>
            <Modal title="查看图片" v-model="visible">
                <img :src="seeImg" v-if="visible" style="width: 100%">
            </Modal>
        </Form-item>
        <Form-item>
            <i-button @click="handleSubmit('formValidate')" type="primary">提交</i-button>
            <i-button type="ghost" @click="jumpPage()" style="margin-left: 8px">返回</i-button>
        </Form-item>
    </i-form>

</template>
<script>
    import apiUtil from '../../assets/utils/api_util';
    import UE from '../../components/ue.vue';

    export default {
        components: {
            UE
        },
        data() {

            return {
                formValidate: {
                    //添加字段
                    title: '',
                    Subtitle: '',
                    Newscontent: '',
                    Viewcount: '',
                    Pubdate: '',
                },
                //ue富文本配置
                config: {
                    initialFrameWidth: null,
                    initialFrameHeight: 350
                },
                ruleValidate: {
                    title: [
                        {required: true, message: '标题不能为空', trigger: 'blur'}
                    ],
                    Subtitle: [
                        {required: true, message: '副标题不能为空', trigger: 'blur'},
                    ],
                    Pubdate: [
                        {required: true, type: 'date', message: '请选择日期', trigger: 'change'}
                    ],
                },
                image: '',
                //图片
                defaultList: [],
                imageList: '',
                imagePost: [],
                visible: false,
                seeImg: '',
                upLoadFile: apiUtil.urls.UploadFile,
                apiUtilBase: apiUtil.urls.baseUrl,
                //富文本
                editor: '',
            }
        },
        // mounted: function () {
        //
        //
        //     this.editor = new Editor("#editor");
        //     this.editor.create();
        // },

        created() {
            let id = this.$route.query.id;

            if (id && id.length > 0) {

                this.getDetail(id)
            }
        },

        methods: {
            //跳转
            jumpPage: function (id, url) {
                this.$router.back(-1)
            },
            handleSubmit(name) {

                let that = this;
                let model = this.$refs[name].model;
                console.log(this.$refs[name].validate())
                this.$refs[name].validate((valid) => {
                    that.upData(model);

                    console.log('dddddfdsfsdfsdf--')

                })
            },
            changeEndDate:function(date){
                console.log(date);
                this.formValidate.Pubdate =date;

            },
            //详情
            getDetail: function (id) {
                let that = this;
                that.$Loading.start();

                let para = {
                    id: id,
                };

                let params = that.$qs.stringify(para);
                apiUtil.get(that,apiUtil.urls.MingDetail, para, function (res) {
                    that.$Loading.finish();
                    that.formValidate.title = res.Msg.Title;
                    that.formValidate.coverpath = res.Msg.Faceimgurl;
                    that.formValidate.Newscontent = res.Msg.Newscontent;
                    that.formValidate.Subtitle = res.Msg.Subtitle;
                    that.formValidate.Viewcount = res.Msg.Viewcount;
                    that.formValidate.Pubdate = res.Msg.Pubdate;


                    console.log('res:',res);
                    let img = {
                        response:{
                            Msg:res.Msg.Faceimgurl
                        },
                        status: 'finished',
                        percentage: '100',
                        showProgress: false
                    };
                    that.$refs.upload.fileList.push(img);
                    that.uploadList();

                });
            },
            //获取富文本内容
            getUEContent() {
                let content = this.$refs.ue.getUEContent();
                return content;
            },
            //编辑
            upData: function (model) {
                let that = this;

                model.Faceimgurl = this.imagePost[0];
                model.id = this.$route.query.id;
                model.Operatorid = '87415a1b-922e-4a94-83c6-8f16c74ab0fb';
                model.Newscontent = this.getUEContent();
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log('bianjicanshu：',params);
                apiUtil.post(that,apiUtil.urls.MingEdit, params, function (res) {
                    that.$Loading.finish();
                    that.$Message.success('编辑成功 ');
                });
            },

            /*
              * 图片
              * */

            uploadList() {
                let that = this;
                this.imageList = that.$refs.upload ? that.$refs.upload.fileList : [];
                console.log('this.imageList:', this.imageList);
            },
            //预览上传图片
            handleView(url) {
                this.seeImg = url;
                this.visible = true;
                console.log('预览照片')
            },
            //删除图片
            handleRemove(file) {
                // 从 upload 实例删除数据
                let that = this;

                that.imagePost = [];

                const fileList = this.$refs.upload.fileList;
                this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    that.imagePost.push(item.response.Msg);
                });
            },
            //上传成功
            handleSuccess(res, file) {
                let that = this;
                file.url = file.response.data;
                that.imagePost = [];

                console.log('this.$refs.upload:', this.$refs.upload.fileList);
                console.log('this.imageList222:', this.imageList);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    that.imagePost.push(item.response.Msg);
                });
                console.log('this.imagePost:', this.imagePost)
            },
            //上传成功
            handleFormatError(file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
                });
            },
            handleMaxSize(file) {
                this.$Notice.warning({
                    title: '超出文件大小限制',
                    desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
                });
            },
            handleBeforeUpload() {
                const check = this.imageList.length < 1;
                if (!check) {
                    this.$Notice.warning({
                        title: '最多只能上传1张图片。'
                    });
                }
                return check;
            }
        }
    }
</script>
